#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/5/10 17:48
# @Author : 闫峰
# @File    : api.py
from typing import List

import requests


class GetSales:

    def get_platform_sales(self, sku_list: List[str], orderSourceType, sku_sales_days):

        sku_list = list(set(sku_list))
        url = """https://api-dw.zhcxkj.com/dw/erp/salesvolume/platform/salessku"""
        if orderSourceType != "all":
            param = {
                "input": {
                    "customerId": 1,
                    "orderSourceType": orderSourceType,
                    "skus": sku_list,
                    "days": sku_sales_days
                }
            }
        else:
            param = {
                "input": {
                    "customerId": 1,
                    "orderSourceType": None,
                    "skus": sku_list,
                    "days": sku_sales_days
                }
            }
        hearders = {
            "Content-Type": "application/json",
            "Authorization": "Basic aXJvYm90Ym94OmkxcjVvOEJvdGJveA==",
        }

        res = requests.post(url=url, headers=hearders, json=param)
        sales = {}
        if isinstance(orderSourceType, int):
            for data in res.json().get('data'):
                if data.get('orderSourceType') == orderSourceType:
                    if data["sku"] not in sales:
                        sales[data["sku"]] = data["salesVolume"]
                    else:
                        sales[data["sku"]] += data["salesVolume"]
            return sales
        else:
            for data in res.json().get('data'):
                if data["sku"] not in sales:
                    sales[data["sku"]] = data["salesVolume"]
                else:
                    sales[data["sku"]] += data["salesVolume"]
            return sales

    def get_source_sku_sales(self, orderSourceType: int, orderSourceSkus: List[str], days):
        """

        :param orderSourceType:
        :param orderSourceSkus:
        :param days: 总销量传None  其他天数传对应的值
        :return:
        """
        orderSourceSkus = list(filter(lambda x: x is not None, orderSourceSkus))
        url = "https://api-dw.zhcxkj.com/dw/erp/salesvolume/platform/ordersourcesku"
        data_json = {
            "input": {
                "customerId": 1,
                "orderSourceSkus": orderSourceSkus,
                "orderSourceType": orderSourceType,
                "days": days
            }
        }
        headers = {
            "Authorization": "Basic aXJvYm90Ym94OmkxcjVvOEJvdGJveA==",
        }
        res = requests.post(url=url, headers=headers, json=data_json)
        data = res.json().get('data')
        if not data:
            return {}
        # {"":""."":""}转化为字典形式
        data = {item.get('orderSourceSku'): item.get('salesVolume') for item in data}
        return data


    def generate_source_sku(self, orderSourceType,orderSourceId,sku_list):
        """
        生成渠道sku
        :return:
        """
        url = f"""http://api-erp.zhcxkj.com/erp/products/createesordersourcesku"""
        headers = {
            "Authorization": "Basic aXJvYm90Ym94OmkxcjVvOEJvdGJveA=="

        }
        data = {
            "orderSourceType": orderSourceType,
            "language": "zh_CN",
            "input": {
                "customerId": 1,
                "orderSourceId": orderSourceId,
                "adminId": 0,
                # "sku": ["1013007", "3090185", "3090185"]
                "sku": sku_list
            }
        }
        resp = requests.post(url=url, headers=headers, json=data)
        data = resp.json().get('data')
        return data

    @staticmethod
    def find_brand(brand_word):
        url = "http://139.159.245.112:9210/sensitive_wipo_distinctbrand/_search"
        headers = {
            "Authorization": "Basic YXBwdXNlcjpvNWVIcmdEM3Vib3BlT2xZIQ=="

        }
        data = {
            "query": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "Brand": brand_word
                            }
                        }
                    ]
                }
            },
            "track_total_hits": True
        }
        res = requests.post(url=url, headers=headers, json=data)
        if res.json()['hits']['total']['value'] == 0:
            return False
        SensitiveId = res.json()['hits']['hits'][0]['_id']
        Brand = res.json()['hits']['hits'][0]['_source']['Brand']
        return SensitiveId,Brand

    @staticmethod
    def find_sensitive_word(sensitive_word):
        url = "http://139.159.245.112:9210/sensitive_erp_distinctsaleword/_search"
        headers = {
            "Authorization": "Basic YXBwdXNlcjpvNWVIcmdEM3Vib3BlT2xZIQ=="

        }
        data = {
            "query": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "Brand": sensitive_word
                            }
                        }
                    ]
                }
            },
            "track_total_hits": True
        }
        res = requests.post(url=url, headers=headers, json=data)
        if res.json()['hits']['total']['value'] == 0:
            return False
        SensitiveId = res.json()['hits']['hits'][0]['_id']
        sensitive_word = res.json()['hits']['hits'][0]['_source']['Brand']
        return SensitiveId,sensitive_word


